/*
                    ConfigFile Class Implementation
*/
#include "ConfigFile.h"

// Initialise static members (default settings)
int ConfigFile::iWindowResX = 1024;
int ConfigFile::iWindowResY = 768;
int ConfigFile::iCurrentWindowResX = 1024;
int ConfigFile::iCurrentWindowResY = 768;
int ConfigFile::iBPP = 32;
bool ConfigFile::bFullscreen = false;

bool ConfigFile::bChaseCam = false;

string ConfigFile::strMetaServer = "localhost";
int ConfigFile::iPort = 8905;

int ConfigFile::iMusicVolume = 0;
int ConfigFile::iSFXVolume = 0;

int ConfigFile::iClientVersion = 1;

string ConfigFile::strBGPath = "data//backgrounds//1024x768.png";
//****************************************

void ConfigFile::ReadFile(string strFilepath)
{
    ifstream cfgFile;

    map<string, string> fileInfo;

    string strLine;

    cfgFile.open(strFilepath.c_str());

    // If the config file couldn't be opened (most likely doesn't exist) return and write a new default file
    if (! cfgFile.is_open())
    {
        printf("CFG Error: Couldn't open configuration file, writing new default file\n");

        cfgFile.close();
        fileInfo.clear();

        WriteFile(strFilepath);

        return;
    }

    while (! cfgFile.eof())
    {
        getline(cfgFile, strLine);

        if ((strLine != "") && (strLine.substr(0, 1) != "*"))
        {
            // Key bindings are marked with a "-" character
            if (strLine.substr(0, 1) == "-")
            {
                KeyBindings::Keylist[lexical_cast<int>(strLine.substr(1, strLine.find("=") - 1))] = strLine.substr(strLine.find("=") + 1);
            }
            else
            {
                fileInfo[strLine.substr(0, strLine.find("="))] = strLine.substr(strLine.find("=") + 1);
            }
        }
    }

    if (fileInfo["windowresx"] != "")       { iWindowResX = lexical_cast<int>(fileInfo["windowresx"]); }
    if (fileInfo["windowresy"] != "")       { iWindowResY = lexical_cast<int>(fileInfo["windowresy"]); }
    if (fileInfo["fullscreen"] != "")       { bFullscreen = lexical_cast<bool>(fileInfo["fullscreen"]); }
    if (fileInfo["chasecam"] != "")         { bChaseCam = lexical_cast<bool>(fileInfo["chasecam"]); }
    if (fileInfo["musicvolume"] != "")      { iMusicVolume = lexical_cast<int>(fileInfo["musicvolume"]); }
    if (fileInfo["sfxvolume"] != "")        { iSFXVolume = lexical_cast<int>(fileInfo["sfxvolume"]); }
    if (fileInfo["metaserver"] != "")       { strMetaServer = fileInfo["metaserver"]; }
    if (fileInfo["port"] != "")             { iPort = lexical_cast<int>(fileInfo["port"]); }

    iCurrentWindowResX = iWindowResX;
    iCurrentWindowResY = iWindowResY;

    fileInfo.clear();
    cfgFile.close();
}

void ConfigFile::WriteFile(string strFilepath)
{
    ofstream cfgFile;

    cfgFile.open(strFilepath.c_str());

    if (!cfgFile.is_open())
    {
        printf("CFG Error: Couldn't write configuration file\n");

        cfgFile.close();

        return;
    }

    cfgFile << "* This is the SnakeWars Config File\n";
    cfgFile << "*\n";
    cfgFile << "* It is highly recommended that you do not modify this file\n";
    cfgFile << "*\n";
    cfgFile << "* If you wish to change settings use the in-game options menu\n\n";

    cfgFile << "windowresx=" << iWindowResX << "\n";
    cfgFile << "windowresy=" << iWindowResY << "\n\n";

    cfgFile << "fullscreen=" << bFullscreen << "\n\n";

    cfgFile << "chasecam=" << bChaseCam << "\n\n";

    cfgFile << "metaserver=" << strMetaServer << "\n";
    cfgFile << "port=" << iPort << "\n\n";

    cfgFile << "musicvolume=" << iMusicVolume << "\n";
    cfgFile << "sfxvolume=" << iSFXVolume << "\n\n";

    cfgFile << "* Key Bindings, do not edit manually.\n";

    for (map<int, string>::iterator iter = KeyBindings::Keylist.begin(); iter != KeyBindings::Keylist.end(); iter++)
    {
        cfgFile << "-" << iter->first << "=" << iter->second << "\n";
    }

    cfgFile.close();
}

